package controllers;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import models.Device;
import models.DeviceModel;
import models.Operator;
import models.Oplog;
import models.RegionLevel;
import models.LevelModelPrice;
import services.AuthorityService;
import services.DeviceModelService;
import services.DeviceService;
import services.LevelModelPriceService;
import services.RegionLevelService;
import utils.BizConstants;
import utils.ExcelUtil;
import utils.FileConfig;
import vos.LevelModelPriceSearchVo;
import vos.LevelModelPriceVo;
import vos.LevelModelPriceVo;
import vos.ReturnCode;
import vos.ReturnListVo;

/**
 * 返款设置操作类
 * 
 * @ClassName: LevelModelPriceController 
 * @author: wangsm
 * @date: 2016年6月4日 上午9:23:00
 */
public class LevelModelPriceController extends BaseController {
	/**
	 * 返款设置页 
	 *
	 * @Title: levelModelPrice 
	 * @return: void
	 */
	public static void levelModelPrice(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取返款设置列表
	 *
	 * @Title: getDealerLevel 
	 * @return: void
	 */
	public static void getLevelModelPrice(int page,int rows, String sort,String order,String levelname,String modelid,String modelname){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		LevelModelPriceSearchVo vo=new LevelModelPriceSearchVo(page,rows,sort,order,levelname,modelid,modelname);
		long total=LevelModelPriceService.countLevelModelPrice(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<LevelModelPrice> lists=LevelModelPriceService.listLevelModelPrice(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDel(List<Map<String,String>> levels){
		if(!checkAuthority("LEVELMODELPRICE_BETCHDEL")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(levels==null){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=LevelModelPriceService.betchDel(levels);
		if(issuccess){
			String model="";
			for(Map<String,String> map :levels){
				model+="城市等级："+map.get("levelid")+",";
				model+="设备类型："+map.get("modelid")+",";
			}
			Operator oper =getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"批量删除返款设置"+model,"返款设置");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 删除记录
	 *
	 * @Title: delLevelModelPrice 
	 * @param id 返款设置id
	 * @return: void
	 */
	public static void delLevelModelPrice(Long levelid,String modelid){
		if(!checkAuthority("LEVELMODELPRICE_DEL")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(levelid==null||StringUtils.isEmpty(modelid)){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录不能为空");
		}
		boolean issuccess=LevelModelPriceService.delLevelModelPrice(levelid,modelid);
		if(issuccess){
			Operator oper =getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"删除返款设置"+levelid+"类型id"+modelid,"返款设置");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 添加返款设置
	 *
	 * @Title: addLevelModelPrice 
	 * @return: void
	 */
	public static void levelModelPriceAdd(){
		render();
	}
	/**
	 * 复制返款设置
	 *
	 * @Title: addLevelModelPrice 
	 * @return: void
	 */
	public static void levelModelPriceCopy(Long levelid,String modelid){
		if(!checkAuthority("LEVELMODELPRICE_COPY")){
			render("errors/404.html");
		}
		if(levelid==null||StringUtils.isEmpty(modelid)){
			render("errors/404.html");
		}
		LevelModelPriceVo oldLevelModelPrice=LevelModelPriceService.getById(levelid,modelid);
		renderArgs.put("levelModelPrice", oldLevelModelPrice);
		render("LevelModelPriceController/levelModelPriceAdd.html");
	}
	/**
	 * 保存返款设置
	 *
	 * @Title: saveLevelModelPrice 
	 * @param levelModelPrice 返款设置对象
	 * @return: void
	 */
	public static void saveLevelModelPrice(LevelModelPrice levelModelPrice){
		if(!checkAuthority("LEVELMODELPRICE_ADD")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(levelModelPrice==null){
			renderFailedJson(ReturnCode.ErrorCode, "返款设置不能为空");
		}
		if(levelModelPrice.remark.length()>255){
			renderFailedJson(ReturnCode.ErrorCode, "备注长度不能大于255");
		}
		RegionLevel level=RegionLevelService.getById(levelModelPrice.levelid);
		if(level==null){
			renderFailedJson(ReturnCode.ErrorCode, "城市等级不存在");
		}
		if(level.isflag){
			renderFailedJson(ReturnCode.ErrorCode, "城市等级被停用");
		}
		DeviceModel model=DeviceModelService.getById(levelModelPrice.modelid);
		if(model==null){
			renderFailedJson(ReturnCode.ErrorCode, "设备型号不存在");
		}
		LevelModelPriceVo oldLevelModelPrice=LevelModelPriceService.getById(levelModelPrice.levelid,levelModelPrice.modelid);
		if(oldLevelModelPrice!=null){
			renderFailedJson(ReturnCode.ErrorCode, "返款信息已存在");
		}
		levelModelPrice.createname=oper.operatorid;
		boolean isSuccess=LevelModelPriceService.saveLevelModelPrice(levelModelPrice);
		if(isSuccess){
			LevelModelPriceVo pvo=LevelModelPriceService.getById(levelModelPrice.levelid,levelModelPrice.modelid);
			Oplog oplog=new Oplog(oper.operatorid,"添加设备返款设置,城市等级："+
			pvo.levelname+",型号："+pvo.modelname+",价格:"+pvo.price,"返款设置");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 编辑返款设置
	 *
	 * @Title: levelModelPriceEdit 
	 * @return: void
	 */
	public static void levelModelPriceEdit(Long levelid,String modelid){
		if(!checkAuthority("LEVELMODELPRICE_EDIT")){
			render("errors/404.html");
		}
		if(levelid==null||StringUtils.isEmpty(modelid)){
			render("errors/404.html");
		}
		LevelModelPriceVo oldLevelModelPrice=LevelModelPriceService.getById(levelid,modelid);
		renderArgs.put("levelModelPrice", oldLevelModelPrice);
		render();
	}
	/**
	 * 查看返款设置
	 *
	 * @Title: levelModelPriceView 
	 * @return: void
	 */
	public static void levelModelPriceView(Long levelid,String modelid){
		if(!checkAuthority("LEVELMODELPRICE_VIEW")){
			render("errors/404.html");
		}
		if(levelid==null||StringUtils.isEmpty(modelid)){
			render("errors/404.html");
		}
		LevelModelPriceVo levelModelPrice=LevelModelPriceService.getById(levelid,modelid);
		renderArgs.put("levelModelPrice", levelModelPrice);
		render();
	}
	/**
	 * 保存返款设置
	 *
	 * @Title: saveLevelModelPrice 
	 * @param levelModelPrice 返款设置对象
	 * @return: void
	 */
	public static void updateLevelModelPrice(LevelModelPrice levelModelPrice){
		if(!checkAuthority("LEVELMODELPRICE_EDIT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(levelModelPrice==null){
			renderFailedJson(ReturnCode.ErrorCode, "返款设置不能为空");
		}
		RegionLevel level=RegionLevelService.getById(levelModelPrice.levelid);
		if(level==null){
			renderFailedJson(ReturnCode.ErrorCode, "城市等级不存在");
		}
		DeviceModel model=DeviceModelService.getById(levelModelPrice.modelid);
		if(model==null){
			renderFailedJson(ReturnCode.ErrorCode, "设备型号不存在");
		}
		LevelModelPriceVo oldLevelModelPrice=LevelModelPriceService.getById(levelModelPrice.levelid,levelModelPrice.modelid);
		if(oldLevelModelPrice==null){
			renderFailedJson(ReturnCode.ErrorCode, "返款信息不存在");
		}
		levelModelPrice.modifyname=oper.operatorid;
		boolean isSuccess=LevelModelPriceService.updateLevelModelPrice(levelModelPrice);
		if(isSuccess){
			LevelModelPriceVo pvo=LevelModelPriceService.getById(levelModelPrice.levelid,levelModelPrice.modelid);
			Oplog oplog=new Oplog(oper.operatorid,"编辑返款设置,城市等级："+
					pvo.levelname+",型号："+pvo.modelname+",价格:"+pvo.price,"返款设置");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param deviceid
	 * @param devicename
	 */
	public static void exportXls(String levelname,String modelid,String modelname){
		if(!checkAuthority("LEVELMODELPRICE_EXPORT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		LevelModelPriceSearchVo vo =new LevelModelPriceSearchVo(levelname,modelid,modelname);
		List<LevelModelPriceVo> lists=LevelModelPriceService.exportXls(vo);
		long y=System.currentTimeMillis();
		String fileName = "返款设置"+y+".csv";
        response.contentType = "application/x-download";
        Operator oper=getLoginUser();
        Oplog oplog=new Oplog(oper.operatorid,"导出返款设置数据","返款设置");
        oplog.save(oplog);
        renderBinary(ExcelUtil.buildExportFile(FileConfig.levelModelPriceXlsTemplate, lists), fileName);
	}
}
